package com.example.grpc.client.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import proto.com.example.grpc.common.MessageProto;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageResponseDTO {
    @JsonProperty("content")
    private String content;
    
    @JsonProperty("receiver")
    private String receiver;
    
    @JsonProperty("timestamp")
    private Long timestamp;
    
    @JsonProperty("success")
    private Boolean success;

    // 从 gRPC 对象转换的静态方法
    public static MessageResponseDTO from(MessageProto.MessageResponse grpcResponse) {
        return new MessageResponseDTO(
            grpcResponse.getContent(),
            grpcResponse.getReceiver(),
            grpcResponse.getTimestamp(),
            grpcResponse.getSuccess()
        );
    }

    // 转换为 gRPC 对象的方法
    public MessageProto.MessageResponse toGrpcMessage() {
        return MessageProto.MessageResponse.newBuilder()
            .setContent(this.content != null ? this.content : "")
            .setReceiver(this.receiver != null ? this.receiver : "")
            .setTimestamp(this.timestamp != null ? this.timestamp : 0L)
            .setSuccess(this.success != null ? this.success : false)
            .build();
    }
}